home *** CD-ROM | disk | FTP | other *** search
/ Aminet 31 / Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso / Aminet / dev / c / vbccppcsrc.lha / vbcc / vlink / vlink.h < prev   
C/C++ Source or Header  |  1999-03-07  |  26KB  |  673 lines

  1. /* $VER: vlink vlink.h V0.6d (13.02.99)
  2.  *
  3.  * This file is part of vlink, a portable linker for multiple
  4.  * object formats.
  5.  * Copyright (c) 1997-99  Frank Wille
  6.  *
  7.  * vlink is freeware and part of the portable and retargetable ANSI C
  8.  * compiler vbcc, copyright (c) 1995-99 by Volker Barthelmann.
  9.  * vlink may be freely redistributed as long as no modifications are
  10.  * made and nothing is charged for it. Non-commercial usage is allowed
  11.  * without any restrictions.
  12.  * EVERY PRODUCT OR PROGRAM DERIVED DIRECTLY FROM MY SOURCE MAY NOT BE
  13.  * SOLD COMMERCIALLY WITHOUT PERMISSION FROM THE AUTHOR.
  14.  *
  15.  *
  16.  * v0.6b (16.01.99) phx
  17.  *       big_endian has to be "signed char" to compile under Irix 5.3.
  18.  * v0.6a (19.12.98) phx
  19.  *       Endianess of linking process will be determined by the type
  20.  *       of the first object.
  21.  *       Support for little endian object file formats.
  22.  *       New macros read16, write16, read32, write32 (replacing the old
  23.  *       macros with the same name) for reading/writing data in the
  24.  *       current endian format.
  25.  * v0.6  (24.10.98) phx
  26.  *       baserel_off disappeared. Each target should know about its
  27.  *       own small data section offset for the base register. The new
  28.  *       field baseoff in FFFuncs was created for this purpose.
  29.  *       New target elf32powerup, which supports the PPC coprocessor
  30.  *       boards from Phase5.
  31.  * v0.5e (05.10.98) phx
  32.  *       Artificial sections and objects, containing longwords sorted
  33.  *       by priority, may be generated using new_priptr().
  34.  *       Reduced hash table size for objects to 1/8 (32 entries).
  35.  * v0.5d (22.08.98) phx
  36.  *       Faster memory allocation can be activated by #define FASTALLOC.
  37.  *       Directories are only scanned if needed (AmigaOS filesystem is
  38.  *       too slow).
  39.  * v0.5c (08.07.98) phx
  40.  *       Automagic generation of @__xxx symbols for amigaehf.
  41.  * v0.5b (05.07.98) phx
  42.  *       Linker symbols for elf32ppcbe.
  43.  *       SYMX_SPECIAL in Symbol->extra flags a linker symbol as
  44.  *       being target-specific
  45.  * v0.5  (27.06.98) phx
  46.  *       Target-specific linker symbol support.
  47.  * v0.4  (05.06.98) phx
  48.  *       New global vars for options: -sc, -sd, -multibase.
  49.  *       New target-specific function targetlink(), which decides whether
  50.  *       two sections must or must not be linked together for a target.
  51.  *       find/create_lnksect() from linker.c are no longer global.
  52.  *       linker_relrefs() finds all relative references between sections.
  53.  *       The sections contain a RelRef-list afterwards, which can be
  54.  *       used to link sections with relative references automatically
  55.  *       together.
  56.  * v0.3b (25.04.98) phx
  57.  *       t_elf.c was split into t_elf32.c and t_elf64.c
  58.  *       New header files: elf64.h and rel_elfalpha.h
  59.  *       Some experimental code for target elf64alpha in t_elf64.c
  60.  * v0.3a (18.04.98) phx
  61.  *       Updated help text.
  62.  * v0.3  (17.04.98) phx
  63.  *       ELF PowerPC 32Bit Big Endian support. Input: objects and
  64.  *       library archives. Output: relocatabable objects.
  65.  *       Full support for little endian file formats by defining new
  66.  *       macros: LECH,LECW,LECVH,LECVW,read16le,read32le,write16le,
  67.  *       write32le (although currently no LE formats are supported).
  68.  *       Replaced l2bh(),l2bw(),read16(),read32(),write16(),write32()
  69.  *       by swap16(),swap32(),read16sw(),read32sw(),write16sw(),write32sw().
  70.  *       fwrite32() is now called fwrite32be().
  71.  *       Section.id may contain a unique identification value.
  72.  *       Included objname in struct LinkFile (useful for ELF archives).
  73.  *       New option -F for reading a list of input files.
  74.  *       Support for ar library archives.
  75.  * v0.2  (07.03.98) phx
  76.  *       Linking of EHF and ADOS seems quite stable. Library units
  77.  *       are linked immediately and are no longer always the last
  78.  *       units in an output file.
  79.  *       insertnode().
  80.  * v0.1  (27.02.98) phx
  81.  *       First version that seems to link AmigaOS ADOS and EHF
  82.  *       objects and libraries. Many common features, like linking
  83.  *       sections together which have relative references, are
  84.  *       still missing. Also, PowerPC-ELF32 support is about to come.
  85.  * v0.0  (04.08.97) phx
  86.  *       File created. Project started on a beautiful summer-day
  87.  *       at the North Sea beach of Cuxhaven. :)
  88.  */
  89.  
  90. #include <stdlib.h>
  91. #include <stdio.h>
  92. #include <string.h>
  93. #include <stdarg.h>
  94. #include <ctype.h>
  95.  
  96. #ifdef HAVE_CONFIG
  97. #include "config.h"
  98. #endif
  99.  
  100. /* program's name */
  101. #define PNAME "vlink"
  102.  
  103. /* version/revision */
  104. #define VERSION 0
  105. #define REVISION 6
  106. #define PLEVEL 4
  107.  
  108.  
  109. /* integer types */
  110. #if defined (TYPES32BIT)
  111. typedef signed char int8;
  112. typedef unsigned char uint8;
  113. typedef signed short int int16;
  114. typedef unsigned short int uint16;
  115. typedef signed long int int32;
  116. typedef unsigned long int uint32;
  117. typedef signed char bool;
  118. #elif defined (TYPES64BIT)
  119. typedef signed char int8;
  120. typedef unsigned char uint8;
  121. typedef signed short int16;
  122. typedef unsigned short uint16;
  123. typedef signed int int32;
  124. typedef unsigned int uint32;
  125. typedef int bool;
  126. #else
  127. #error Unsupported architecture! Define either TYPES32BIT or TYPES64BIT.
  128. #endif
  129.  
  130.  
  131. /* endian conversion */
  132. #if defined (BIGENDIAN)
  133. #define ECH(x) x
  134. #define ECW(x) x
  135. #define ECVH(x) x
  136. #define ECVW(x) x
  137. #define read16be(x) (*(uint16 *)(x))
  138. #define read32be(x) (*(uint32 *)(x))
  139. #define write16be(x,y) (*(uint16 *)(x)=(y))
  140. #define write32be(x,y) (*(uint32 *)(x)=(y))
  141. #define LECH(x) (((x)&0xff)<<8|((x)&0xff00)>>8)
  142. #define LECW(x) (((x)&0xff)<<24|((x)&0xff00)<<8|((x)&0xff0000)>>8|((x)&0xff000000)>>24)
  143. #define LECVH(x) swap16(x)
  144. #define LECVW(x) swap32(x)
  145. #define read16le(x) read16sw(x)
  146. #define read32le(x) read32sw(x)
  147. #define write16le(x,y) write16sw(x,y)
  148. #define write32le(x,y) write32sw(x,y)
  149.  
  150. #elif defined (LITTLEENDIAN)
  151. #define ECH(x) (((x)&0xff)<<8|((x)&0xff00)>>8)
  152. #define ECW(x) (((x)&0xff)<<24|((x)&0xff00)<<8|((x)&0xff0000)>>8|((x)&0xff000000)>>24)
  153. #define ECVH(x) swap16(x)
  154. #define ECVW(x) swap32(x)
  155. #define read16be(x) read16sw(x)
  156. #define read32be(x) read32sw(x)
  157. #define write16be(x,y) write16sw(x,y)
  158. #define write32be(x,y) write32sw(x,y)
  159. #define LECH(x) x
  160. #define LECW(x) x
  161. #define LECVH(x) x
  162. #define LECVW(x) x
  163. #define read16le(x) (*(uint16 *)(x))
  164. #define read32le(x) (*(uint32 *)(x))
  165. #define write16le(x,y) (*(uint16 *)(x)=(y))
  166. #define write32le(x,y) (*(uint32 *)(x)=(y))
  167. #else
  168. #error You have to define either BIGENDIAN or LITTLEENDIAN.
  169. #endif
  170.  
  171. /* read/write data in current endianess */
  172. #define read16(e,x) ((e)?(read16be(x)):(read16le(x)))
  173. #define read32(e,x) ((e)?(read32be(x)):(read32le(x)))
  174. #define write16(e,x,y) if(e) write16be(x,y); else write16le(x,y)
  175. #define write32(e,x,y) if(e) write32be(x,y); else write32le(x,y)
  176.  
  177. /* program constants */
  178.  
  179. #ifndef TRUE
  180. #define TRUE 1
  181. #endif
  182. #ifndef FALSE
  183. #define FALSE 0
  184. #endif
  185. #ifndef NULL
  186. #define NULL 0
  187. #endif
  188.  
  189. #define FNAMEBUFSIZE 1024       /* buffer size for file names */
  190. #define MAX_FWALIGN 8192        /* max. aligment, when writing target file */
  191.  
  192.  
  193. /* structures */
  194.  
  195. struct node {
  196.   struct node *next;
  197.   struct node *pred;
  198. };
  199.  
  200. struct list {
  201.   struct node *first;
  202.   struct node *dummy;
  203.   struct node *last;
  204. };
  205.  
  206.  
  207. struct LibPath {                /* libpaths list. */
  208.   struct node n;                /* Here we search for library archives */
  209.   char *path;                   /* and shared objects. */
  210. };
  211.  
  212.  
  213. struct InputFile {              /* inputlist nodes */
  214.   struct node n;                /* contains names & flags of all inp. files */
  215.   char *name;
  216.   bool lib;                     /* search library */
  217.   bool dynamic;                 /* try to link dynamic first */
  218.   int so_ver;                   /* minimum version of shared object */
  219. };
  220.  
  221.  
  222. struct LinkFile {
  223.   struct node n;
  224.   char *pathname;               /* full path: /usr/lib/libm.a */
  225.   char *filename;               /* file name: libm.a */
  226.   char *objname;                /* current obj. name: sin.o (archives only)*/
  227.   uint8 *data;                  /* pointer to file data */
  228.   unsigned long length;         /* length of file */
  229.   uint8 format;                 /* file format - index into targets table */
  230.   uint8 type;                   /* ID_OBJECT/SHAREDOBJ/LIBARCH */
  231. };
  232.  
  233.  
  234. struct ObjectUnit {
  235.   struct node n;
  236.